package com.itheima.demo003;

import java.util.Scanner;

public class test24 {
    public static void main(String[] args) {
        //1.键盘录入一个字符串
        Scanner sc = new Scanner(System.in);
        String str;

        while (true) {
            System.out.println("请输入一个字符串");
            str = sc.next();

            //2.检验字符串是否满足规则
            boolean flag = checkStr(str);
            if(flag){
                break;
            }else{
                System.out.println("当前字符串不符合规则，请重新输入");
                continue;
            }
        }

        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < str.length(); i++){
            char c = str.charAt(i);
            int number = c - 48;//数字1,2,3
            String s = changLuoMa(c);//number
            sb.append(s + " ");
            //System.out.print(s + " ");
        }
        System.out.println(sb);

    }

    /*//查表法：定义一个数组，让索引和罗马数字，产生一个对应关系
    public static String changLuoMa(int number) {
        String[] arr = {"", "Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", "Ⅶ", "Ⅷ", "Ⅸ"};
        return arr[number];
    }
    */

    //利用switch进行匹配  jdk12，新特性
    public static String changLuoMa(char number){
        String str = switch (number){
            case '0' -> "";
            case '1' -> "Ⅰ";
            case '2' -> "Ⅱ";
            case '3' -> "Ⅲ";
            case '4' -> "Ⅳ";
            case '5' -> "Ⅴ";
            case '6' -> "Ⅵ";
            case '7' -> "Ⅶ";
            case '8' -> "Ⅷ";
            case '9' -> "Ⅸ";
            default -> str = "";
        };

        return str;
    }



    public static boolean checkStr (String str){
        //要求一：长度为小于等于九
        if (str.length() > 9) {
            return false;
        }
        //要求二：只能是数字
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);//0~9
            if (c < '0' || c > '9') {
                return false;
            }
        }
        return true;
    }
}